package com.yys.gulimall.order.interceptor;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.yys.common.constant.AutherServerConstant;
import com.yys.common.to.user.MemberVo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author zq
 * @version 1.0
 * @date 2021/4/3 14:08
 */
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        //放行查询订单状态
        boolean match = new AntPathMatcher()
                .match("/order/order/status/**", request.getRequestURI());
        boolean match2 = new AntPathMatcher()
                .match("/payed/**", request.getRequestURI());
        if(match || match2) {
            return true;
        }



        HttpSession session = request.getSession();
        MemberVo memberVo = (MemberVo) session.getAttribute(AutherServerConstant.LOGIN_USER);
        if(ObjectUtils.isNotNull(memberVo)) {
            threadLocal.set(memberVo);
            return true;
        } else {
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
